package com.example.cookbook.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.cookbook.R;
import com.example.cookbook.fragment.OneFragment;

public class TagAdapter extends RecyclerView.Adapter<TagAdapter.ViewHolder>  {
    public static final String[] DEFAULT_TAGS = {"蛋类","虾","螃蟹","鱼","贝","烘焙","奶制品","甜品","特色食品",};
    private final OneFragment fragment;
    private String[] tags = DEFAULT_TAGS;
    private int selectedTag = 0;

    public TagAdapter(OneFragment fragment){
        this.fragment = fragment;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layouttagtext,viewGroup,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, int i) {
        viewHolder.text.setText(tags[i]);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            /** @noinspection deprecation*/
            @Override
            public void onClick(View view) {
                fragment.notifyOnTagClick(tags[viewHolder.getAdapterPosition()]);
                TagAdapter.this.selectedTag = viewHolder.getAdapterPosition();
                TagAdapter.this.notifyDataSetChanged();
            }
        });
        if(selectedTag == i){
            viewHolder.text.setBackgroundResource(R.drawable.my_round_rectangle2);
        }else{
            viewHolder.text.setBackgroundResource(R.drawable.my_round_rectangle1);
        }
    }
    @Override
    public int getItemCount() {
        return tags.length;
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView text;
        public ViewHolder(View itemView) {
            super(itemView);
            this.text = itemView.findViewById(R.id.text);
        }
    }

}